package org.deng.learn.basic.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author DengLibin
 * @Description nio操作文件
 * @Date 2018/11/15 0015 15:03
 */
public class FileMain {

    public  static  void main(String[] args) throws IOException {
      //  wireText();
      //  readText();
        copyFile();
    }

    //写入文本
    public  static  void wireText() throws IOException {
        FileOutputStream fio = new FileOutputStream("C:\\Users\\DengLibin\\Desktop\\niofile.txt");

        //获取通道
        FileChannel channel = fio.getChannel();
        //创建缓冲区
        ByteBuffer bb = ByteBuffer.allocate(1024);
        bb.put("你好,我来自中国！".getBytes("utf-8")); // 将给定的字节写入此缓冲区的当前位置，然后该位置递增
        System.out.println("缓冲区位置:"+bb.position());
        bb.flip();//位置归零 它将限制设置为当前位置，然后将位置设置为 0
        int write = channel.write(bb);
        System.out.println("写入结果："+write);
        channel.close();
        fio.close();
    }

    //读取文本
    public  static  void readText() throws IOException {
        FileInputStream fin = new FileInputStream("C:\\Users\\DengLibin\\Desktop\\niofile.txt");
        FileChannel channel = fin.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        int read = channel.read(byteBuffer);
        System.out.println("读取结果:"+read);
        System.out.println("位置:"+byteBuffer.position()+",limit:"+byteBuffer.limit()); //25 1024
        byteBuffer.flip();//此时limit=position，position=0
        System.out.println("位置:"+byteBuffer.position()+",limit:"+byteBuffer.limit()); //0 25

        byte[] bu = new byte[1024];
        int index = 0;
        byte[] array = byteBuffer.array();
        System.out.println("array长度："+array.length); //1024
        while (byteBuffer.remaining() > 0){
            byte b = byteBuffer.get();
            bu[index] = b;
            index++;
        }
        channel.close();
        fin.close();
        System.out.println("结果:"+new String(bu,0,index,"UTF-8"));
        System.out.println("结果:"+new String(array,"UTF-8"));
    }

    //复制文件

    public  static  void copyFile() throws IOException {
        FileInputStream fin = new FileInputStream("C:\\Users\\DengLibin\\Desktop\\123.jpg");
        FileChannel inChannel = fin.getChannel();


        FileOutputStream fou = new FileOutputStream("C:\\Users\\DengLibin\\Desktop\\123_copy.jpg");
        FileChannel outChannel = fou.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        int read = 0;
        int total = 0;
        while((read = inChannel.read(byteBuffer)) > 0){ //达到末尾返回-1
            System.out.println("读取后,position:"+byteBuffer.position()+",limit:"+byteBuffer.limit()); //position:1024,limit:1024
            byteBuffer.flip();
            System.out.println("读取后归零,position:"+byteBuffer.position()+",limit:"+byteBuffer.limit());//position:0,limit:1024

            outChannel.write(byteBuffer);
            System.out.println("写入后,position:"+byteBuffer.position()+",limit:"+byteBuffer.limit()); //position:1024,limit:1024
            byteBuffer.flip();
            System.out.println("写入后归零,position:"+byteBuffer.position()+",limit:"+byteBuffer.limit());//position:0,limit:1024
            total+= read;

        }
        inChannel.close();
        fin.close();
        outChannel.close();
        fou.close();
        System.out.println("完成，字节总数： "+total+" byte");

    }
}
